<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"><title>目标检测 | 常用数据集标注格式及生成脚本 | Justlovesmile's BLOG</title><meta name="keywords" content="深度学习,目标检测,python"><meta name="author" content="Justlovesmile,865717150@qq.com"><meta name="copyright" content="Justlovesmile"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="目标检测是计算机视觉任务中的一个重要研究方向，是计算机视觉的根本性问题之一，是其他诸多计算机视觉任务的基础以及前提。本文主要介绍了目标检测数据集的几种标注格式和转换代码。"><meta property="og:type" content="article"><meta property="og:title" content="目标检测 | 常用数据集标注格式及生成脚本"><meta property="og:url" content="https://blog.justlovesmile.top/posts/865c56ba.html"><meta property="og:site_name" content="Justlovesmile&#39;s BLOG"><meta property="og:description" content="目标检测是计算机视觉任务中的一个重要研究方向，是计算机视觉的根本性问题之一，是其他诸多计算机视觉任务的基础以及前提。本文主要介绍了目标检测数据集的几种标注格式和转换代码。"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/202109111517311.jpg"><meta property="article:published_time" content="2021-09-11T07:11:26.000Z"><meta property="article:modified_time" content="2021-09-11T07:11:26.000Z"><meta property="article:author" content="Justlovesmile"><meta property="article:tag" content="深度学习"><meta property="article:tag" content="目标检测"><meta property="article:tag" content="python"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/202109111517311.jpg"><link rel="shortcut icon" href="/img/logo.jpg"><link rel="canonical" href="https://blog.justlovesmile.top/posts/865c56ba"><link rel="preconnect" href="//cdn.jsdelivr.net"><link rel="preconnect" href="//hm.baidu.com"><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload='this.media="all"'><script>var _hmt=_hmt||[];!function(){var e=document.createElement("script");e.src="https://hm.baidu.com/hm.js?a2ee893562999ebad688b0d82daa100a";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)}()</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN/font/family=Titillium+Web.css" media="print" onload='this.media="all"'><script>const GLOBAL_CONFIG={root:"/",algolia:void 0,localSearch:{path:"search.xml",languages:{hits_empty:"找不到您查询的内容：${query}"}},translate:void 0,noticeOutdate:void 0,highlight:{plugin:"highlighjs",highlightCopy:!0,highlightLang:!0,highlightHeightLimit:400},copy:{success:"复制成功",error:"复制错误",noSupport:"浏览器不支持"},relativeDate:{homepage:!1,post:!1},runtime:"天",date_suffix:{just:"刚刚",min:"分钟前",hour:"小时前",day:"天前",month:"个月前"},copyright:{limitCount:100,languages:{author:"作者: Justlovesmile",link:"链接: ",source:"来源: Justlovesmile's BLOG",info:"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},lightbox:"fancybox",Snackbar:{chs_to_cht:"你已切换为繁体",cht_to_chs:"你已切换为简体",day_to_night:"你已切换为深色模式",night_to_day:"你已切换为浅色模式",bgLight:"var(--mj-card-bg)",bgDark:"var(--mj-card-bg)",position:"top-right"},source:{justifiedGallery:{js:"https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js",css:"https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css"}},isPhotoFigcaption:!1,islazyload:!0,isAnchor:!1}</script><script id="config-diff">var GLOBAL_CONFIG_SITE={title:"目标检测 | 常用数据集标注格式及生成脚本",isPost:!0,isHome:!1,isHighlightShrink:!1,isToc:!0,postUpdate:"2021-09-11 15:11:26"}</script><noscript><style>#nav{opacity:1}.justified-gallery img{opacity:1}#post-meta time,#recent-posts time{display:inline!important}</style></noscript><script>(e=>{e.saveToLocal={set:function(e,t,o){if(0===o)return;const a=864e5*o,n={value:t,expiry:(new Date).getTime()+a};localStorage.setItem(e,JSON.stringify(n))},get:function(e){const t=localStorage.getItem(e);if(!t)return;const o=JSON.parse(t);if(!((new Date).getTime()>o.expiry))return o.value;localStorage.removeItem(e)}},e.getScript=e=>new Promise((t,o)=>{const a=document.createElement("script");a.src=e,a.async=!0,a.onerror=o,a.onload=a.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(a.onload=a.onreadystatechange=null,t())},document.head.appendChild(a)}),e.activateDarkMode=function(){document.documentElement.setAttribute("data-theme","dark"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#0d0d0d")},e.activateLightMode=function(){document.documentElement.setAttribute("data-theme","light"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#ffffff")};const t=saveToLocal.get("theme"),o=(new Date).getHours();void 0===t?o<=6||o>=18?activateDarkMode():activateLightMode():"light"===t?activateLightMode():activateDarkMode();/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)&&document.documentElement.classList.add("apple")})(window)</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome-animation@0.2.1/dist/font-awesome-animation.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="/css/justlovesmile.css"><link rel="stylesheet" href="/css/blogicon.css"><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="Justlovesmile's BLOG" type="application/atom+xml"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="" data-lazy-src="/img/avatar.jpg" onerror='onerror=null,src="/img/friend_404.gif"' alt="avatar"></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">75</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">69</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div></div><hr><div class="menus_items"><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><span>网站</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/"><i class="fa-fw fas fa-home"></i> <span>首页</span></a></li><li><a class="site-page child" href="/guestbook/"><i class="fa-fw fas fa-pencil-alt"></i> <span>留言</span></a></li><li><a class="site-page child" href="/friends/"><i class="fa-fw fas fa-paper-plane"></i> <span>友链</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><span>文库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i> <span>全部标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i> <span>全部分类</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-calendar"></i> <span>文章列表</span></a></li><li><a class="site-page child" href="/random/"><i class="fa-fw fas fa-shoe-prints"></i> <span>随便逛逛</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><span>实验室</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/laboratory/"><i class="fa-fw fa fa-lightbulb-o"></i> <span>项目展示</span></a></li><li><a class="site-page child" href="/fcircle/"><i class="fa-fw fa fa-puzzle-piece"></i> <span>友链订阅</span></a></li><li><a class="site-page child" href="/charts/"><i class="fa-fw fa fa-pie-chart"></i> <span>博客统计</span></a></li><li><a class="site-page child" href="/update/"><i class="fa-fw fa fa-commenting-o"></i> <span>更新日志</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><span>清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-camera-retro"></i> <span>相册</span></a></li><li><a class="site-page child" href="/video/"><i class="fa-fw fa fa-video-camera"></i> <span>视频</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i> <span>歌单</span></a></li><li><a class="site-page child" href="/focus/"><i class="fa-fw fa fa-check-square-o"></i> <span>关注</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><span>关于</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/about/"><i class="fa-fw fas fa-user"></i> <span>本站</span></a></li><li><a class="site-page child" href="/love/"><i class="fa-fw fa fa-heart"></i> <span>Love</span></a></li><li><a class="site-page child" href="/donate/"><i class="fa-fw fa fa-gratipay"></i> <span>打赏</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image:url(https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/202109111517311.jpg)"><nav id="nav"><div id="nav-group"><div id="blog_name"><a id="site-name" href="/">Justlovesmile</a></div><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><span>网站</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/"><i class="fa-fw fas fa-home"></i> <span>首页</span></a></li><li><a class="site-page child" href="/guestbook/"><i class="fa-fw fas fa-pencil-alt"></i> <span>留言</span></a></li><li><a class="site-page child" href="/friends/"><i class="fa-fw fas fa-paper-plane"></i> <span>友链</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><span>文库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i> <span>全部标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i> <span>全部分类</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-calendar"></i> <span>文章列表</span></a></li><li><a class="site-page child" href="/random/"><i class="fa-fw fas fa-shoe-prints"></i> <span>随便逛逛</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><span>实验室</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/laboratory/"><i class="fa-fw fa fa-lightbulb-o"></i> <span>项目展示</span></a></li><li><a class="site-page child" href="/fcircle/"><i class="fa-fw fa fa-puzzle-piece"></i> <span>友链订阅</span></a></li><li><a class="site-page child" href="/charts/"><i class="fa-fw fa fa-pie-chart"></i> <span>博客统计</span></a></li><li><a class="site-page child" href="/update/"><i class="fa-fw fa fa-commenting-o"></i> <span>更新日志</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><span>清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-camera-retro"></i> <span>相册</span></a></li><li><a class="site-page child" href="/video/"><i class="fa-fw fa fa-video-camera"></i> <span>视频</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i> <span>歌单</span></a></li><li><a class="site-page child" href="/focus/"><i class="fa-fw fa fa-check-square-o"></i> <span>关注</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><span>关于</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/about/"><i class="fa-fw fas fa-user"></i> <span>本站</span></a></li><li><a class="site-page child" href="/love/"><i class="fa-fw fa fa-heart"></i> <span>Love</span></a></li><li><a class="site-page child" href="/donate/"><i class="fa-fw fa fa-gratipay"></i> <span>打赏</span></a></li></ul></div></div></div><div id="nav-right"><div id="search-button"><a class="nav-rightbutton site-page social-icon search"><i class="fas fa-search fa-fw"></i></a></div><div id="darkmode_navswitch"><a class="nav-rightbutton site-page darkmode_switchbutton" onclick="switchDarkMode()"><i class="fas fa-adjust"></i></a></div><div id="toggle-menu"><a class="nav-rightbutton site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></div></nav><div class="coverdiv" id="coverdiv"><img class="cover entered loading" id="post-cover" alt="cover" src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/202109111517311.jpg"></div><div id="post-info"><div class="post-firstinfo" id="post-meta"><span class="post-meta-categories"><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/">人工智能</a></span><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">#深度学习</a><a class="post-meta__tags" href="/tags/%E7%9B%AE%E6%A0%87%E6%A3%80%E6%B5%8B/">#目标检测</a><a class="post-meta__tags" href="/tags/python/">#python</a></div></div><h1 class="post-title">目标检测 | 常用数据集标注格式及生成脚本</h1><div id="post-meta"><div class="meta-firstline"><span class="meta-share-time"><span class="meta-avatar"><a class="meta-avatar-img" href="/about/" title="关于作者"><img alt="作者头像" src="" data-lazy-src="/img/avatar.jpg"></a><a class="meta-avatar-name" href="/about/" title="关于作者">Justlovesmile</a></span></span><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">发表于</span><time datetime="2021-09-11T07:11:26.000Z" title="发表于 2021-09-11 15:11:26">2021-09-11</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">4.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>21分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>目标检测是计算机视觉任务中的一个重要研究方向，其用于解决对数码图像中特定种类的可视目标实例的检测问题。目标检测作为计算机视觉的根本性问题之一，是其他诸多计算机视觉任务，例如图像描述生成，实例分割和目标跟踪的基础以及前提。而在解决此类问题时，我们常常需要使用自己的脚本或者利用标注工具生成数据集，数据集格式往往会多种多样，因此对于目标检测任务而言，为了更好地兼容训练，大多数目标检测模型框架会默认支持几种常用的数据集标注格式，常见的分别是COCO，Pascal VOC，YOLO等等。本文主要介绍上述几种数据集格式以及我写的Python脚本（一般需要根据实际情况再改改）。</p><h1 id="1-COCO"><a href="#1-COCO" class="headerlink" title="1. COCO"></a>1. COCO</h1><h2 id="1-1-COCO数据集格式"><a href="#1-1-COCO数据集格式" class="headerlink" title="1.1 COCO数据集格式"></a>1.1 COCO数据集格式</h2><p>COCO（Common Objects in COtext）数据集，是一个大规模的，适用于目标检测，图像分割，Image Captioning任务的数据集，其标注格式是最常用的几种格式之一。目前使用较多的是COCO2017数据集。其官网为<a target="_blank" rel="external nofollow noopener noreferrer" href="https://cocodataset.org/">COCO - Common Objects in Context (cocodataset.org)</a>。</p><p><img src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/202109111535004.png" alt="image-20210911153516753"></p><p>COCO数据集主要包含图像（jpg或者png等等）和标注文件（json），其数据集格式如下(<code>/</code>代表文件夹)：</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">-coco/</span><br><span class="line">    |-train2017/</span><br><span class="line">    	|-1.jpg</span><br><span class="line">    	|-2.jpg</span><br><span class="line">    |-val2017/</span><br><span class="line">    	|-3.jpg</span><br><span class="line">    	|-4.jpg</span><br><span class="line">    |-test2017/</span><br><span class="line">    	|-5.jpg</span><br><span class="line">    	|-6.jpg</span><br><span class="line">    |-annotations/</span><br><span class="line">    	|-instances_train2017.json</span><br><span class="line">    	|-instances_val2017.json</span><br><span class="line">    	|-*.json</span><br></pre></td></tr></table></figure><p><code>train2017</code>以及<code>val2017</code>这两个文件夹中存储的是训练集和验证集的图像，而<code>test2017</code>文件夹中存储的是测试集的信息，可以只是图像，也可以包含标注，一般是单独使用的。</p><p><code>annotations</code>文件夹中的文件就是标注文件，如果你有<code>xml</code>文件，通常需要转换成<code>json</code>格式，其格式如下（更详细的可以参考<a target="_blank" rel="external nofollow noopener noreferrer" href="https://cocodataset.org/#format-data">官网</a>）：</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="attr">&quot;info&quot;</span>: info, </span><br><span class="line">	<span class="attr">&quot;images&quot;</span>: [image], <span class="comment">//列表</span></span><br><span class="line">	<span class="attr">&quot;annotations&quot;</span>: [annotation], <span class="comment">//列表</span></span><br><span class="line">	<span class="attr">&quot;categories&quot;</span>: [category], <span class="comment">//列表</span></span><br><span class="line">	<span class="attr">&quot;licenses&quot;</span>: [license], <span class="comment">//列表</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>其中<code>info</code>为整个数据集的信息，包括年份，版本，描述等等信息，如果只是完成训练任务，其实不太重要，如下所示：</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//对于训练，不是那么的重要</span></span><br><span class="line">info&#123;</span><br><span class="line">	<span class="attr">&quot;year&quot;</span>: int, </span><br><span class="line">	<span class="attr">&quot;version&quot;</span>: str, </span><br><span class="line">	<span class="attr">&quot;description&quot;</span>: str, </span><br><span class="line">	<span class="attr">&quot;contributor&quot;</span>: str, </span><br><span class="line">	<span class="attr">&quot;url&quot;</span>: str, </span><br><span class="line">	<span class="attr">&quot;date_created&quot;</span>: datetime,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>其中的<code>image</code>为图像的基本信息，包括序号，宽高，文件名等等信息，其中的序号（<code>id</code>）需要和后面的<code>annotations</code>中的标注所属图片序号对应如下所示：</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">image&#123;</span><br><span class="line">	<span class="attr">&quot;id&quot;</span>: int, <span class="comment">//必要</span></span><br><span class="line">	<span class="attr">&quot;width&quot;</span>: int, <span class="comment">//必要</span></span><br><span class="line">	<span class="attr">&quot;height&quot;</span>: int, <span class="comment">//必要</span></span><br><span class="line">	<span class="attr">&quot;file_name&quot;</span>: str, <span class="comment">//必要</span></span><br><span class="line">	<span class="attr">&quot;license&quot;</span>: int,</span><br><span class="line">	<span class="attr">&quot;flickr_url&quot;</span>: str,</span><br><span class="line">	<span class="attr">&quot;coco_url&quot;</span>: str,</span><br><span class="line">	<span class="attr">&quot;date_captured&quot;</span>: datetime, </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>其中的<code>annotation</code>是最重要的标注信息，包括序号，所属图像序号，类别序号等等信息，如下所示：</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">annotation&#123;</span><br><span class="line">	<span class="attr">&quot;id&quot;</span>: int, <span class="comment">//标注id</span></span><br><span class="line">	<span class="attr">&quot;image_id&quot;</span>: int, <span class="comment">//所属图像id</span></span><br><span class="line">	<span class="attr">&quot;category_id&quot;</span>: int, <span class="comment">//类别id</span></span><br><span class="line">	<span class="attr">&quot;segmentation&quot;</span>: RLE or [polygon], <span class="comment">//图像分割标注</span></span><br><span class="line">	<span class="attr">&quot;area&quot;</span>: float, <span class="comment">//区域面积</span></span><br><span class="line">	<span class="attr">&quot;bbox&quot;</span>: [x,y,width,height], <span class="comment">//目标框左上角坐标以及宽高</span></span><br><span class="line">	<span class="attr">&quot;iscrowd&quot;</span>: <span class="number">0</span> or <span class="number">1</span>, <span class="comment">//是否密集</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>其中的<code>category</code>代表类别信息，包括父类别，类别序号以及类别名称，如下所示：</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">category&#123;</span><br><span class="line">	<span class="attr">&quot;id&quot;</span>: int, <span class="comment">//类别序号</span></span><br><span class="line">	<span class="attr">&quot;name&quot;</span>: str, <span class="comment">//类别名称</span></span><br><span class="line">	<span class="attr">&quot;supercategory&quot;</span>: str, <span class="comment">//父类别</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>其中的<code>license</code>代表数据集的协议许可信息，包括序号，协议名称以及链接信息，如下所示：</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//对于训练，不重要</span></span><br><span class="line">license&#123;</span><br><span class="line">	<span class="attr">&quot;id&quot;</span>: int, </span><br><span class="line">	<span class="attr">&quot;name&quot;</span>: str, </span><br><span class="line">	<span class="attr">&quot;url&quot;</span>: str,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>接下来，我们来看一个简单的示例：</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line"><span class="attr">&quot;info&quot;</span>: &#123;略&#125;, <span class="attr">&quot;images&quot;</span>: [&#123;<span class="attr">&quot;id&quot;</span>: <span class="number">1</span>, <span class="attr">&quot;file_name&quot;</span>: <span class="string">&quot;1.jpg&quot;</span>, <span class="attr">&quot;height&quot;</span>: <span class="number">334</span>, <span class="attr">&quot;width&quot;</span>: <span class="number">500</span>&#125;, &#123;<span class="attr">&quot;id&quot;</span>: <span class="number">2</span>, <span class="attr">&quot;file_name&quot;</span>: <span class="string">&quot;2.jpg&quot;</span>, <span class="attr">&quot;height&quot;</span>: <span class="number">445</span>, <span class="attr">&quot;width&quot;</span>: <span class="number">556</span>&#125;], <span class="attr">&quot;annotations&quot;</span>: [&#123;<span class="attr">&quot;id&quot;</span>: <span class="number">1</span>, <span class="attr">&quot;area&quot;</span>: <span class="number">40448</span>, <span class="attr">&quot;iscrowd&quot;</span>: <span class="number">0</span>, <span class="attr">&quot;image_id&quot;</span>: <span class="number">1</span>, <span class="attr">&quot;bbox&quot;</span>: [<span class="number">246</span>, <span class="number">61</span>, <span class="number">128</span>, <span class="number">316</span>], <span class="attr">&quot;category_id&quot;</span>: <span class="number">3</span>, <span class="attr">&quot;segmentation&quot;</span>: []&#125;, &#123;<span class="attr">&quot;id&quot;</span>: <span class="number">2</span>, <span class="attr">&quot;area&quot;</span>: <span class="number">40448</span>, <span class="attr">&quot;iscrowd&quot;</span>: <span class="number">0</span>, <span class="attr">&quot;image_id&quot;</span>: <span class="number">1</span>, <span class="attr">&quot;bbox&quot;</span>: [<span class="number">246</span>, <span class="number">61</span>, <span class="number">128</span>, <span class="number">316</span>], <span class="attr">&quot;category_id&quot;</span>: <span class="number">2</span>, <span class="attr">&quot;segmentation&quot;</span>: []&#125;, &#123;<span class="attr">&quot;id&quot;</span>: <span class="number">3</span>, <span class="attr">&quot;area&quot;</span>: <span class="number">40448</span>, <span class="attr">&quot;iscrowd&quot;</span>: <span class="number">0</span>, <span class="attr">&quot;image_id&quot;</span>: <span class="number">2</span>, <span class="attr">&quot;bbox&quot;</span>: [<span class="number">246</span>, <span class="number">61</span>, <span class="number">128</span>, <span class="number">316</span>], <span class="attr">&quot;category_id&quot;</span>: <span class="number">1</span>, <span class="attr">&quot;segmentation&quot;</span>: []&#125;], <span class="attr">&quot;categories&quot;</span>: [&#123;<span class="attr">&quot;supercategory&quot;</span>: <span class="string">&quot;none&quot;</span>, <span class="attr">&quot;id&quot;</span>: <span class="number">1</span>, <span class="attr">&quot;name&quot;</span>: <span class="string">&quot;liner&quot;</span>&#125;,&#123;<span class="attr">&quot;supercategory&quot;</span>: <span class="string">&quot;none&quot;</span>, <span class="attr">&quot;id&quot;</span>: <span class="number">2</span>, <span class="attr">&quot;name&quot;</span>: <span class="string">&quot;containership&quot;</span>&#125;,&#123;<span class="attr">&quot;supercategory&quot;</span>: <span class="string">&quot;none&quot;</span>, <span class="attr">&quot;id&quot;</span>: <span class="number">3</span>, <span class="attr">&quot;name&quot;</span>: <span class="string">&quot;bulkcarrier&quot;</span>&#125;], <span class="attr">&quot;licenses&quot;</span>: [&#123;略&#125;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h2 id="1-2-COCO转换脚本"><a href="#1-2-COCO转换脚本" class="headerlink" title="1.2 COCO转换脚本"></a>1.2 COCO转换脚本</h2><p><code>Python转换脚本</code>如下所示，需要准备<code>图像</code>和<code>xml</code>标注文件：</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Author    : justlovesmile</span></span><br><span class="line"><span class="comment"># @Date      : 2021/9/8 15:36</span></span><br><span class="line"><span class="keyword">import</span> os, random, json</span><br><span class="line"><span class="keyword">import</span> shutil <span class="keyword">as</span> sh</span><br><span class="line"><span class="keyword">from</span> tqdm.auto <span class="keyword">import</span> tqdm</span><br><span class="line"><span class="keyword">import</span> xml.etree.ElementTree <span class="keyword">as</span> xmlET</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">mkdir</span>(<span class="params">path</span>):</span></span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(path):</span><br><span class="line">        os.makedirs(path)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;The path (<span class="subst">&#123;path&#125;</span>) already exists.&quot;</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">readxml</span>(<span class="params">file</span>):</span></span><br><span class="line">    tree = xmlET.parse(file)</span><br><span class="line">    <span class="comment">#图片尺寸字段</span></span><br><span class="line">    size = tree.find(<span class="string">&#x27;size&#x27;</span>)</span><br><span class="line">    width = <span class="built_in">int</span>(size.find(<span class="string">&#x27;width&#x27;</span>).text)</span><br><span class="line">    height = <span class="built_in">int</span>(size.find(<span class="string">&#x27;height&#x27;</span>).text)</span><br><span class="line">    <span class="comment">#目标字段</span></span><br><span class="line">    objs = tree.findall(<span class="string">&#x27;object&#x27;</span>)</span><br><span class="line">    bndbox = []</span><br><span class="line">    <span class="keyword">for</span> obj <span class="keyword">in</span> objs:</span><br><span class="line">        label = obj.find(<span class="string">&quot;name&quot;</span>).text</span><br><span class="line">        bnd = obj.find(<span class="string">&quot;bndbox&quot;</span>)</span><br><span class="line">        xmin = <span class="built_in">int</span>(bnd.find(<span class="string">&quot;xmin&quot;</span>).text)</span><br><span class="line">        ymin = <span class="built_in">int</span>(bnd.find(<span class="string">&quot;ymin&quot;</span>).text)</span><br><span class="line">        xmax = <span class="built_in">int</span>(bnd.find(<span class="string">&quot;xmax&quot;</span>).text)</span><br><span class="line">        ymax = <span class="built_in">int</span>(bnd.find(<span class="string">&quot;ymax&quot;</span>).text)</span><br><span class="line">        bbox = [xmin, ymin, xmax, ymax, label]</span><br><span class="line">        bndbox.append(bbox)</span><br><span class="line">    <span class="keyword">return</span> [[width, height], bndbox]</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">tococo</span>(<span class="params">xml_root, image_root, output_root,classes=&#123;&#125;,errorId=[],train_percent=<span class="number">0.9</span></span>):</span></span><br><span class="line">    <span class="comment"># assert</span></span><br><span class="line">    <span class="keyword">assert</span> train_percent&lt;=<span class="number">1</span> <span class="keyword">and</span> <span class="built_in">len</span>(classes)&gt;<span class="number">0</span></span><br><span class="line">    <span class="comment"># define the root path</span></span><br><span class="line">    train_root = os.path.join(output_root, <span class="string">&quot;train2017&quot;</span>)</span><br><span class="line">    val_root = os.path.join(output_root, <span class="string">&quot;val2017&quot;</span>)</span><br><span class="line">    ann_root = os.path.join(output_root, <span class="string">&quot;annotations&quot;</span>)</span><br><span class="line">    <span class="comment"># initialize train and val dict</span></span><br><span class="line">    train_content = &#123;</span><br><span class="line">        <span class="string">&quot;images&quot;</span>: [],  <span class="comment"># &#123;&quot;file_name&quot;: &quot;09780.jpg&quot;, &quot;height&quot;: 334, &quot;width&quot;: 500, &quot;id&quot;: 9780&#125;</span></span><br><span class="line">        <span class="string">&quot;annotations&quot;</span>: [],<span class="comment"># &#123;&quot;area&quot;: 40448, &quot;iscrowd&quot;: 0, &quot;image_id&quot;: 1, &quot;bbox&quot;: [246, 61, 128, 316], &quot;category_id&quot;: 5, &quot;id&quot;: 1, &quot;segmentation&quot;: []&#125;</span></span><br><span class="line">        <span class="string">&quot;categories&quot;</span>: []  <span class="comment"># &#123;&quot;supercategory&quot;: &quot;none&quot;, &quot;id&quot;: 1, &quot;name&quot;: &quot;liner&quot;&#125;</span></span><br><span class="line">    &#125;</span><br><span class="line">    val_content = &#123;</span><br><span class="line">        <span class="string">&quot;images&quot;</span>: [],  <span class="comment"># &#123;&quot;file_name&quot;: &quot;09780.jpg&quot;, &quot;height&quot;: 334, &quot;width&quot;: 500, &quot;id&quot;: 9780&#125;</span></span><br><span class="line">        <span class="string">&quot;annotations&quot;</span>: [],<span class="comment"># &#123;&quot;area&quot;: 40448, &quot;iscrowd&quot;: 0, &quot;image_id&quot;: 1, &quot;bbox&quot;: [246, 61, 128, 316], &quot;category_id&quot;: 5, &quot;id&quot;: 1, &quot;segmentation&quot;: []&#125;</span></span><br><span class="line">        <span class="string">&quot;categories&quot;</span>: []  <span class="comment"># &#123;&quot;supercategory&quot;: &quot;none&quot;, &quot;id&quot;: 1, &quot;name&quot;: &quot;liner&quot;&#125;</span></span><br><span class="line">    &#125;</span><br><span class="line">    train_json = <span class="string">&#x27;instances_train2017.json&#x27;</span></span><br><span class="line">    val_json = <span class="string">&#x27;instances_val2017.json&#x27;</span></span><br><span class="line">    <span class="comment"># divide the trainset and valset</span></span><br><span class="line">    images = os.listdir(image_root)</span><br><span class="line">    total_num = <span class="built_in">len</span>(images)</span><br><span class="line">    train_percent = train_percent</span><br><span class="line">    train_num = <span class="built_in">int</span>(total_num * train_percent)</span><br><span class="line">    train_file = <span class="built_in">sorted</span>(random.sample(images, train_num))</span><br><span class="line">    <span class="keyword">if</span> mkdir(output_root):</span><br><span class="line">        <span class="keyword">if</span> mkdir(train_root) <span class="keyword">and</span> mkdir(val_root) <span class="keyword">and</span> mkdir(ann_root):</span><br><span class="line">            idx1, idx2, dx1, dx2 = <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span></span><br><span class="line">            <span class="keyword">for</span> file <span class="keyword">in</span> tqdm(images):</span><br><span class="line">                name=os.path.splitext(os.path.basename(file))[<span class="number">0</span>]</span><br><span class="line">                <span class="keyword">if</span> name <span class="keyword">not</span> <span class="keyword">in</span> errorId:</span><br><span class="line">                    res = readxml(os.path.join(xml_root, name + <span class="string">&#x27;.xml&#x27;</span>))</span><br><span class="line">                    <span class="keyword">if</span> file <span class="keyword">in</span> train_file:</span><br><span class="line">                        idx1 += <span class="number">1</span></span><br><span class="line">                        sh.copy(os.path.join(image_root, file), train_root)</span><br><span class="line">                        train_content[<span class="string">&#x27;images&#x27;</span>].append(</span><br><span class="line">                            &#123;<span class="string">&quot;file_name&quot;</span>: file, <span class="string">&quot;width&quot;</span>: res[<span class="number">0</span>][<span class="number">0</span>], <span class="string">&quot;height&quot;</span>: res[<span class="number">0</span>][<span class="number">1</span>], <span class="string">&quot;id&quot;</span>: idx1&#125;)</span><br><span class="line">                        <span class="keyword">for</span> b <span class="keyword">in</span> res[<span class="number">1</span>]:</span><br><span class="line">                            dx1 += <span class="number">1</span></span><br><span class="line">                            x = b[<span class="number">0</span>]</span><br><span class="line">                            y = b[<span class="number">1</span>]</span><br><span class="line">                            w = b[<span class="number">2</span>] - b[<span class="number">0</span>]</span><br><span class="line">                            h = b[<span class="number">3</span>] - b[<span class="number">1</span>]</span><br><span class="line">                            train_content[<span class="string">&#x27;annotations&#x27;</span>].append(</span><br><span class="line">                                &#123;<span class="string">&quot;area&quot;</span>: w * h, <span class="string">&quot;iscrowd&quot;</span>: <span class="number">0</span>, <span class="string">&quot;image_id&quot;</span>: idx1, <span class="string">&quot;bbox&quot;</span>: [x, y, w, h],</span><br><span class="line">                                 <span class="string">&quot;category_id&quot;</span>: classes[b[<span class="number">4</span>]], <span class="string">&quot;id&quot;</span>: dx1, <span class="string">&quot;segmentation&quot;</span>: []&#125;)</span><br><span class="line">                    <span class="keyword">else</span>:</span><br><span class="line">                        idx2 += <span class="number">1</span></span><br><span class="line">                        sh.copy(os.path.join(image_root, file), val_root)</span><br><span class="line">                        val_content[<span class="string">&#x27;images&#x27;</span>].append(</span><br><span class="line">                            &#123;<span class="string">&quot;file_name&quot;</span>: file, <span class="string">&quot;width&quot;</span>: res[<span class="number">0</span>][<span class="number">0</span>], <span class="string">&quot;height&quot;</span>: res[<span class="number">0</span>][<span class="number">1</span>], <span class="string">&quot;id&quot;</span>: idx2&#125;)</span><br><span class="line">                        <span class="keyword">for</span> b <span class="keyword">in</span> res[<span class="number">1</span>]:</span><br><span class="line">                            dx2 += <span class="number">1</span></span><br><span class="line">                            x = b[<span class="number">0</span>]</span><br><span class="line">                            y = b[<span class="number">1</span>]</span><br><span class="line">                            w = b[<span class="number">2</span>] - b[<span class="number">0</span>]</span><br><span class="line">                            h = b[<span class="number">3</span>] - b[<span class="number">1</span>]</span><br><span class="line">                            val_content[<span class="string">&#x27;annotations&#x27;</span>].append(</span><br><span class="line">                                &#123;<span class="string">&quot;area&quot;</span>: w * h, <span class="string">&quot;iscrowd&quot;</span>: <span class="number">0</span>, <span class="string">&quot;image_id&quot;</span>: idx2, <span class="string">&quot;bbox&quot;</span>: [x, y, w, h],</span><br><span class="line">                                 <span class="string">&quot;category_id&quot;</span>: classes[b[<span class="number">4</span>]], <span class="string">&quot;id&quot;</span>: dx2, <span class="string">&quot;segmentation&quot;</span>: []&#125;)</span><br><span class="line">            <span class="keyword">for</span> i, j <span class="keyword">in</span> classes.items():</span><br><span class="line">                train_content[<span class="string">&#x27;categories&#x27;</span>].append(&#123;<span class="string">&quot;supercategory&quot;</span>: <span class="string">&quot;none&quot;</span>, <span class="string">&quot;id&quot;</span>: j, <span class="string">&quot;name&quot;</span>: i&#125;)</span><br><span class="line">                val_content[<span class="string">&#x27;categories&#x27;</span>].append(&#123;<span class="string">&quot;supercategory&quot;</span>: <span class="string">&quot;none&quot;</span>, <span class="string">&quot;id&quot;</span>: j, <span class="string">&quot;name&quot;</span>: i&#125;)</span><br><span class="line">            <span class="keyword">with</span> <span class="built_in">open</span>(os.path.join(ann_root, train_json), <span class="string">&#x27;w&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">                json.dump(train_content, f)</span><br><span class="line">            <span class="keyword">with</span> <span class="built_in">open</span>(os.path.join(ann_root, val_json), <span class="string">&#x27;w&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">                json.dump(val_content, f)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;Number of Train Images:&quot;</span>, <span class="built_in">len</span>(os.listdir(train_root)))</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;Number of Val Images:&quot;</span>, <span class="built_in">len</span>(os.listdir(val_root)))</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">test</span>():</span></span><br><span class="line">    box_root = <span class="string">&quot;E:/MyProject/Dataset/hwtest/annotations&quot;</span> <span class="comment">#xml文件夹</span></span><br><span class="line">    image_root = <span class="string">&quot;E:/MyProject/Dataset/hwtest/images&quot;</span> <span class="comment">#image文件夹</span></span><br><span class="line">    output_root = <span class="string">&quot;E:/MyProject/Dataset/coco&quot;</span> <span class="comment">#输出文件夹</span></span><br><span class="line">    classes = &#123;<span class="string">&quot;liner&quot;</span>: <span class="number">0</span>,<span class="string">&quot;bulk carrier&quot;</span>: <span class="number">1</span>,<span class="string">&quot;warship&quot;</span>: <span class="number">2</span>,<span class="string">&quot;sailboat&quot;</span>: <span class="number">3</span>,<span class="string">&quot;canoe&quot;</span>: <span class="number">4</span>,<span class="string">&quot;container ship&quot;</span>: <span class="number">5</span>,<span class="string">&quot;fishing boat&quot;</span>: <span class="number">6</span>&#125; <span class="comment">#类别字典</span></span><br><span class="line">    errorId = [] <span class="comment">#脏数据id</span></span><br><span class="line">    train_percent = <span class="number">0.9</span> <span class="comment">#训练集和验证集比例</span></span><br><span class="line">    tococo(box_root, image_root, output_root,classes=classes,errorId=errorId,train_percent=train_percent)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    test()</span><br></pre></td></tr></table></figure><h1 id="2-VOC"><a href="#2-VOC" class="headerlink" title="2. VOC"></a>2. VOC</h1><h2 id="2-1-VOC数据集格式"><a href="#2-1-VOC数据集格式" class="headerlink" title="2.1 VOC数据集格式"></a>2.1 VOC数据集格式</h2><p>VOC（Visual Object Classes）数据集来源于PASCAL VOC挑战赛，其主要任务有<code>Object Classification</code> 、<code>Object Detection</code>、<code>Object Segmentation</code>、<code>Human Layout</code>、<code>Action Classification</code>。其官网为<a target="_blank" rel="external nofollow noopener noreferrer" href="http://host.robots.ox.ac.uk/pascal/VOC/">The PASCAL Visual Object Classes Homepage (ox.ac.uk)</a>。其主要数据集有VOC2007以及VOC2012。</p><p><img src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/202109111939729.png" alt="image-20210911193933398"></p><p>VOC数据集主要包含图像（jpg或者png等等）和标注文件（xml），其数据集格式如下(<code>/</code>代表文件夹)：</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">-VOC/</span><br><span class="line">	|-JPEGImages/</span><br><span class="line">		|-1.jpg</span><br><span class="line">		|-2.jpg</span><br><span class="line">	|-Annotations/</span><br><span class="line">		|-1.xml</span><br><span class="line">		|-2.xml</span><br><span class="line">	|-ImageSets/</span><br><span class="line">		|-Layout/</span><br><span class="line">			|-*.txt</span><br><span class="line">		|-Main/</span><br><span class="line">			|-train.txt</span><br><span class="line">			|-val.txt</span><br><span class="line">			|-trainval.txt</span><br><span class="line">			|-test.txt</span><br><span class="line">		|-Segmentation/</span><br><span class="line">			|-*.txt</span><br><span class="line">		|-Action/</span><br><span class="line">			|-*.txt</span><br><span class="line">	|-SegmentationClass/</span><br><span class="line">	|-SegmentationObject/</span><br></pre></td></tr></table></figure><p>其中对于目标检测任务而言，最常用的以及必须的文件夹包括：<code>JPEGImages</code>，<code>Annotations</code>，<code>ImageSets/Main</code>。</p><p><code>JPEGImages</code>里存放的是图像，而<code>Annotations</code>里存放的是<code>xml</code>标注文件，文件内容如下：</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">&lt;annotation&gt;</span><br><span class="line">	&lt;folder&gt;VOC&lt;/folder&gt;            # 图像所在文件夹</span><br><span class="line">	&lt;filename&gt;000032.jpg&lt;/filename&gt; # 图像文件名</span><br><span class="line">	&lt;source&gt;                        # 图像源</span><br><span class="line">		&lt;database&gt;The VOC Database&lt;/database&gt;</span><br><span class="line">		&lt;annotation&gt;PASCAL VOC&lt;/annotation&gt;</span><br><span class="line">		&lt;image&gt;flickr&lt;/image&gt;</span><br><span class="line">	&lt;/source&gt;</span><br><span class="line">	&lt;size&gt;                          # 图像尺寸信息</span><br><span class="line">		&lt;width&gt;500&lt;/width&gt;    # 图像宽度</span><br><span class="line">		&lt;height&gt;281&lt;/height&gt;  # 图像高度</span><br><span class="line">		&lt;depth&gt;3&lt;/depth&gt;      # 图像通道数</span><br><span class="line">	&lt;/size&gt;</span><br><span class="line">	&lt;segmented&gt;0&lt;/segmented&gt;  # 图像是否用于分割，0代表不适用，对目标检测而言没关系</span><br><span class="line">	&lt;object&gt;                  # 一个目标对象的信息</span><br><span class="line">		&lt;name&gt;aeroplane&lt;/name&gt;    # 目标的类别名</span><br><span class="line">		&lt;pose&gt;Frontal&lt;/pose&gt;      # 拍摄角度，若无一般为Unspecified</span><br><span class="line">		&lt;truncated&gt;0&lt;/truncated&gt;  # 是否被截断，0表示完整未截断</span><br><span class="line">		&lt;difficult&gt;0&lt;/difficult&gt;  # 是否难以识别，0表示不难识别</span><br><span class="line">		&lt;bndbox&gt;            # 边界框信息</span><br><span class="line">			&lt;xmin&gt;104&lt;/xmin&gt;  # 左上角x</span><br><span class="line">			&lt;ymin&gt;78&lt;/ymin&gt;   # 左上角y</span><br><span class="line">			&lt;xmax&gt;375&lt;/xmax&gt;  # 右下角x</span><br><span class="line">			&lt;ymax&gt;183&lt;/ymax&gt;  # 右下角y</span><br><span class="line">		&lt;/bndbox&gt;</span><br><span class="line">	&lt;/object&gt;</span><br><span class="line">    # 下面是其他目标的信息，这里略掉</span><br><span class="line">	&lt;object&gt;</span><br><span class="line">        其他object信息，这里省略</span><br><span class="line">	&lt;/object&gt;</span><br><span class="line">&lt;/annotation&gt;</span><br></pre></td></tr></table></figure><h2 id="2-2-VOC转换脚本"><a href="#2-2-VOC转换脚本" class="headerlink" title="2.2 VOC转换脚本"></a>2.2 VOC转换脚本</h2><p>下面这个脚本，只适用于有图像和xml文件的情况下，coco转voc格式以后有需要再写：</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Author    : justlovesmile</span></span><br><span class="line"><span class="comment"># @Date      : 2021/9/8 21:01</span></span><br><span class="line"><span class="keyword">import</span> os,random</span><br><span class="line"><span class="keyword">from</span> tqdm.auto <span class="keyword">import</span> tqdm</span><br><span class="line"><span class="keyword">import</span> shutil <span class="keyword">as</span> sh</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">mkdir</span>(<span class="params">path</span>):</span></span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(path):</span><br><span class="line">        os.mkdir(path)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;The path (<span class="subst">&#123;path&#125;</span>) already exists.&quot;</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">tovoc</span>(<span class="params">xmlroot,imgroot,saveroot,errorId=[],classes=&#123;&#125;,tvp=<span class="number">1.0</span>,trp=<span class="number">0.9</span></span>):</span></span><br><span class="line">    <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">    参数：</span></span><br><span class="line"><span class="string">        root：数据集存放根目录</span></span><br><span class="line"><span class="string">    功能：</span></span><br><span class="line"><span class="string">        加载数据，并保存为VOC格式</span></span><br><span class="line"><span class="string">    加载后的格式：</span></span><br><span class="line"><span class="string">    VOC/</span></span><br><span class="line"><span class="string">      Annotations/</span></span><br><span class="line"><span class="string">        - **.xml</span></span><br><span class="line"><span class="string">      JPEGImages/</span></span><br><span class="line"><span class="string">        - **.jpg</span></span><br><span class="line"><span class="string">      ImageSets/</span></span><br><span class="line"><span class="string">        Main/</span></span><br><span class="line"><span class="string">          - train.txt</span></span><br><span class="line"><span class="string">          - test.txt</span></span><br><span class="line"><span class="string">          - val.txt</span></span><br><span class="line"><span class="string">          - trainval.txt</span></span><br><span class="line"><span class="string">    &#x27;&#x27;&#x27;</span></span><br><span class="line">    <span class="comment"># assert</span></span><br><span class="line">    <span class="keyword">assert</span> <span class="built_in">len</span>(classes)&gt;<span class="number">0</span></span><br><span class="line">    <span class="comment"># init path</span></span><br><span class="line">    VOC = saveroot</span><br><span class="line">    ann_path = os.path.join(VOC, <span class="string">&#x27;Annotations&#x27;</span>)</span><br><span class="line">    img_path = os.path.join(VOC,<span class="string">&#x27;JPEGImages&#x27;</span>)</span><br><span class="line">    set_path = os.path.join(VOC,<span class="string">&#x27;ImageSets&#x27;</span>)</span><br><span class="line">    txt_path = os.path.join(set_path,<span class="string">&#x27;Main&#x27;</span>)</span><br><span class="line">    <span class="comment"># mkdirs </span></span><br><span class="line">    <span class="keyword">if</span> mkdir(VOC):</span><br><span class="line">        <span class="keyword">if</span> mkdir(ann_path) <span class="keyword">and</span> mkdir(img_path) <span class="keyword">and</span> mkdir(set_path):</span><br><span class="line">            mkdir(txt_path)</span><br><span class="line"></span><br><span class="line">    images = os.listdir(imgroot)</span><br><span class="line">    list_index = <span class="built_in">range</span>(<span class="built_in">len</span>(images))</span><br><span class="line">    <span class="comment">#test and trainval set</span></span><br><span class="line">    trainval_percent = tvp</span><br><span class="line">    train_percent = trp</span><br><span class="line">    val_percent = <span class="number">1</span> - train_percent <span class="keyword">if</span> train_percent&lt;<span class="number">1</span> <span class="keyword">else</span> <span class="number">0.1</span></span><br><span class="line">    total_num = <span class="built_in">len</span>(images)</span><br><span class="line">    trainval_num = <span class="built_in">int</span>(total_num*trainval_percent)</span><br><span class="line">    train_num = <span class="built_in">int</span>(trainval_num*train_percent)</span><br><span class="line">    val_num = <span class="built_in">int</span>(trainval_num*val_percent) <span class="keyword">if</span> train_percent&lt;<span class="number">1</span> <span class="keyword">else</span> <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    trainval = random.sample(list_index,trainval_num)</span><br><span class="line">    train = random.sample(list_index,train_num)</span><br><span class="line">    val = random.sample(list_index,val_num)</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> tqdm(list_index):</span><br><span class="line">        imgfile = images[i]</span><br><span class="line">        img_id = os.path.splitext(os.path.basename(imgfile))[<span class="number">0</span>]</span><br><span class="line">        xmlfile = img_id+<span class="string">&quot;.xml&quot;</span></span><br><span class="line">        sh.copy(os.path.join(imgroot,imgfile),os.path.join(img_path,imgfile))</span><br><span class="line">        sh.copy(os.path.join(xmlroot,xmlfile),os.path.join(ann_path,xmlfile))</span><br><span class="line">        <span class="keyword">if</span> img_id <span class="keyword">not</span> <span class="keyword">in</span> errorId:</span><br><span class="line">            <span class="keyword">if</span> i <span class="keyword">in</span> trainval:</span><br><span class="line">                <span class="keyword">with</span> <span class="built_in">open</span>(os.path.join(txt_path,<span class="string">&#x27;trainval.txt&#x27;</span>),<span class="string">&#x27;a&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">                    f.write(img_id+<span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line">                <span class="keyword">if</span> i <span class="keyword">in</span> train:</span><br><span class="line">                    <span class="keyword">with</span> <span class="built_in">open</span>(os.path.join(txt_path,<span class="string">&#x27;train.txt&#x27;</span>),<span class="string">&#x27;a&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">                        f.write(img_id+<span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line">                <span class="keyword">else</span>:</span><br><span class="line">                    <span class="keyword">with</span> <span class="built_in">open</span>(os.path.join(txt_path,<span class="string">&#x27;val.txt&#x27;</span>),<span class="string">&#x27;a&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">                        f.write(img_id+<span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line">                <span class="keyword">if</span> train_percent==<span class="number">1</span> <span class="keyword">and</span> i <span class="keyword">in</span> val:</span><br><span class="line">                    <span class="keyword">with</span> <span class="built_in">open</span>(os.path.join(txt_path,<span class="string">&#x27;val.txt&#x27;</span>),<span class="string">&#x27;a&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">                        f.write(img_id+<span class="string">&#x27;\n&#x27;</span>)          </span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                <span class="keyword">with</span> <span class="built_in">open</span>(os.path.join(txt_path,<span class="string">&#x27;test.txt&#x27;</span>),<span class="string">&#x27;a&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">                    f.write(img_id+<span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># end</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;Dataset to VOC format finished!&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">test</span>():</span></span><br><span class="line">    box_root = <span class="string">&quot;E:/MyProject/Dataset/hwtest/annotations&quot;</span></span><br><span class="line">    image_root = <span class="string">&quot;E:/MyProject/Dataset/hwtest/images&quot;</span></span><br><span class="line">    output_root = <span class="string">&quot;E:/MyProject/Dataset/voc&quot;</span></span><br><span class="line">    classes = &#123;<span class="string">&quot;liner&quot;</span>: <span class="number">0</span>,<span class="string">&quot;bulk carrier&quot;</span>: <span class="number">1</span>,<span class="string">&quot;warship&quot;</span>: <span class="number">2</span>,<span class="string">&quot;sailboat&quot;</span>: <span class="number">3</span>,<span class="string">&quot;canoe&quot;</span>: <span class="number">4</span>,<span class="string">&quot;container ship&quot;</span>: <span class="number">5</span>,<span class="string">&quot;fishing boat&quot;</span>: <span class="number">6</span>&#125;</span><br><span class="line">    errorId = []</span><br><span class="line">    train_percent = <span class="number">0.9</span></span><br><span class="line">    tovoc(box_root,image_root,output_root,errorId,classes,trp=train_percent)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    test()</span><br></pre></td></tr></table></figure><h1 id="3-YOLO"><a href="#3-YOLO" class="headerlink" title="3. YOLO"></a>3. YOLO</h1><h2 id="3-1-YOLO数据集格式"><a href="#3-1-YOLO数据集格式" class="headerlink" title="3.1 YOLO数据集格式"></a>3.1 YOLO数据集格式</h2><p><code>YOLO</code>数据集格式的出现主要是为了训练<code>YOLO</code>模型，其文件格式没有固定的要求，因为可以通过修改模型的配置文件进行数据加载，唯一需要注意的是<code>YOLO</code>数据集的标注格式是将目标框的位置信息进行归一化处理（此处归一化指的是除以图片宽和高），如下所示：</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;目标类别&#125; &#123;归一化后的目标中心点x坐标&#125; &#123;归一化后的目标中心点y坐标&#125; &#123;归一化后的目标框宽度w&#125; &#123;归一化后的目标框高度h&#125;</span><br></pre></td></tr></table></figure><h2 id="3-2-YOLO转换脚本"><a href="#3-2-YOLO转换脚本" class="headerlink" title="3.2 YOLO转换脚本"></a>3.2 YOLO转换脚本</h2><p><code>Python</code>转换脚本如下所示：</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Author    : justlovesmile</span></span><br><span class="line"><span class="comment"># @Date      : 2021/9/8 20:28</span></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"><span class="keyword">from</span> tqdm.auto <span class="keyword">import</span> tqdm</span><br><span class="line"><span class="keyword">import</span> shutil <span class="keyword">as</span> sh</span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="keyword">import</span> xml.etree.cElementTree <span class="keyword">as</span> et</span><br><span class="line"><span class="keyword">except</span> ImportError:</span><br><span class="line">    <span class="keyword">import</span> xml.etree.ElementTree <span class="keyword">as</span> et</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">mkdir</span>(<span class="params">path</span>):</span></span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(path):</span><br><span class="line">        os.makedirs(path)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;The path (<span class="subst">&#123;path&#125;</span>) already exists.&quot;</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">False</span>  </span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">xml2yolo</span>(<span class="params">xmlpath,savepath,classes=&#123;&#125;</span>):</span></span><br><span class="line">    namemap = classes</span><br><span class="line">    <span class="comment">#try:</span></span><br><span class="line">    <span class="comment">#    with open(&#x27;classes_yolo.json&#x27;,&#x27;r&#x27;) as f:</span></span><br><span class="line">    <span class="comment">#        namemap=json.load(f)</span></span><br><span class="line">    <span class="comment">#except:</span></span><br><span class="line">    <span class="comment">#    pass</span></span><br><span class="line">    rt = et.parse(xmlpath).getroot()</span><br><span class="line">    w = <span class="built_in">int</span>(rt.find(<span class="string">&quot;size&quot;</span>).find(<span class="string">&quot;width&quot;</span>).text)</span><br><span class="line">    h = <span class="built_in">int</span>(rt.find(<span class="string">&quot;size&quot;</span>).find(<span class="string">&quot;height&quot;</span>).text)</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(savepath, <span class="string">&quot;w&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        <span class="keyword">for</span> obj <span class="keyword">in</span> rt.findall(<span class="string">&quot;object&quot;</span>):</span><br><span class="line">            name = obj.find(<span class="string">&quot;name&quot;</span>).text</span><br><span class="line">            xmin = <span class="built_in">int</span>(obj.find(<span class="string">&quot;bndbox&quot;</span>).find(<span class="string">&quot;xmin&quot;</span>).text)</span><br><span class="line">            ymin = <span class="built_in">int</span>(obj.find(<span class="string">&quot;bndbox&quot;</span>).find(<span class="string">&quot;ymin&quot;</span>).text)</span><br><span class="line">            xmax = <span class="built_in">int</span>(obj.find(<span class="string">&quot;bndbox&quot;</span>).find(<span class="string">&quot;xmax&quot;</span>).text)</span><br><span class="line">            ymax = <span class="built_in">int</span>(obj.find(<span class="string">&quot;bndbox&quot;</span>).find(<span class="string">&quot;ymax&quot;</span>).text)</span><br><span class="line">            f.write(</span><br><span class="line">                <span class="string">f&quot;<span class="subst">&#123;namemap[name]&#125;</span> <span class="subst">&#123;(xmin+xmax)/w/<span class="number">2.</span>&#125;</span> <span class="subst">&#123;(ymin+ymax)/h/<span class="number">2.</span>&#125;</span> <span class="subst">&#123;(xmax-xmin)/w&#125;</span> <span class="subst">&#123;(ymax-ymin)/h&#125;</span>&quot;</span></span><br><span class="line">                + <span class="string">&quot;\n&quot;</span></span><br><span class="line">            )</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">trainval</span>(<span class="params">xmlroot,imgroot,saveroot,errorId=[],classes=&#123;&#125;,tvp=<span class="number">1.0</span>,trp=<span class="number">0.9</span></span>):</span></span><br><span class="line">    <span class="comment"># assert</span></span><br><span class="line">    <span class="keyword">assert</span> tvp&lt;=<span class="number">1.0</span> <span class="keyword">and</span> trp &lt;=<span class="number">1.0</span> <span class="keyword">and</span> <span class="built_in">len</span>(classes)&gt;<span class="number">0</span></span><br><span class="line">    <span class="comment"># create dirs</span></span><br><span class="line">    imglabel = [<span class="string">&#x27;images&#x27;</span>,<span class="string">&#x27;labels&#x27;</span>]</span><br><span class="line">    trainvaltest = [<span class="string">&#x27;train&#x27;</span>,<span class="string">&#x27;val&#x27;</span>,<span class="string">&#x27;test&#x27;</span>]</span><br><span class="line">    mkdir(saveroot)</span><br><span class="line">    <span class="keyword">for</span> r <span class="keyword">in</span> imglabel:</span><br><span class="line">        mkdir(os.path.join(saveroot,r))</span><br><span class="line">        <span class="keyword">for</span> s <span class="keyword">in</span> trainvaltest:</span><br><span class="line">            mkdir(os.path.join(saveroot,r,s))</span><br><span class="line">    <span class="comment">#train / val</span></span><br><span class="line">    trainval_percent = tvp</span><br><span class="line">    train_percent = trp</span><br><span class="line">    val_percent = <span class="number">1</span> - train_percent <span class="keyword">if</span> train_percent&lt;<span class="number">1.0</span> <span class="keyword">else</span> <span class="number">0.15</span></span><br><span class="line">    </span><br><span class="line">    total_img = os.listdir(imgroot)</span><br><span class="line">    num = <span class="built_in">len</span>(total_img)</span><br><span class="line">    list_index = <span class="built_in">range</span>(num)</span><br><span class="line">    tv = <span class="built_in">int</span>(num * trainval_percent)</span><br><span class="line">    tr = <span class="built_in">int</span>(tv * train_percent)</span><br><span class="line">    va = <span class="built_in">int</span>(tv * val_percent)</span><br><span class="line">    trainval = random.sample(list_index, tv) <span class="comment"># trainset and valset</span></span><br><span class="line">    train = random.sample(trainval, tr) <span class="comment"># trainset</span></span><br><span class="line">    val = random.sample(trainval, va) <span class="comment">#valset, use it only when train_percent = 1 </span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;trainval_percent:<span class="subst">&#123;trainval_percent&#125;</span>,train_percent:<span class="subst">&#123;train_percent&#125;</span>,val_percent:<span class="subst">&#123;val_percent&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> tqdm(list_index):</span><br><span class="line">        name = total_img[i]</span><br><span class="line">        op = os.path.join(imgroot,name)</span><br><span class="line">        file_id = os.path.splitext(os.path.basename(name))[<span class="number">0</span>]</span><br><span class="line">        <span class="keyword">if</span> file_id <span class="keyword">not</span> <span class="keyword">in</span> errorId:</span><br><span class="line">            xmlp = os.path.join(xmlroot,file_id+<span class="string">&#x27;.xml&#x27;</span>)</span><br><span class="line">            <span class="keyword">if</span> i <span class="keyword">in</span> trainval:</span><br><span class="line">                <span class="comment"># trainset and valset</span></span><br><span class="line">                <span class="keyword">if</span> i <span class="keyword">in</span> train:</span><br><span class="line">                    sp = os.path.join(saveroot,<span class="string">&quot;images&quot;</span>,<span class="string">&quot;train&quot;</span>,name)</span><br><span class="line">                    xml2yolo(xmlp,os.path.join(saveroot,<span class="string">&quot;labels&quot;</span>,<span class="string">&quot;train&quot;</span>,file_id+<span class="string">&#x27;.txt&#x27;</span>),classes)</span><br><span class="line">                    sh.copy(op,sp)</span><br><span class="line">                <span class="keyword">else</span>:</span><br><span class="line">                    sp = os.path.join(saveroot,<span class="string">&quot;images&quot;</span>,<span class="string">&quot;val&quot;</span>,name)</span><br><span class="line">                    xml2yolo(xmlp,os.path.join(saveroot,<span class="string">&quot;labels&quot;</span>,<span class="string">&quot;val&quot;</span>,file_id+<span class="string">&#x27;.txt&#x27;</span>),classes)</span><br><span class="line">                    sh.copy(op,sp)</span><br><span class="line">                <span class="keyword">if</span> (train_percent==<span class="number">1.0</span> <span class="keyword">and</span> i <span class="keyword">in</span> val):</span><br><span class="line">                    sp = os.path.join(saveroot,<span class="string">&quot;images&quot;</span>,<span class="string">&quot;val&quot;</span>,name)</span><br><span class="line">                    xml2yolo(xmlp,os.path.join(saveroot,<span class="string">&quot;labels&quot;</span>,<span class="string">&quot;val&quot;</span>,file_id+<span class="string">&#x27;.txt&#x27;</span>),classes)</span><br><span class="line">                    sh.copy(op,sp)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                <span class="comment"># testset</span></span><br><span class="line">                sp = os.path.join(saveroot,<span class="string">&quot;images&quot;</span>,<span class="string">&quot;test&quot;</span>,name)</span><br><span class="line">                xml2yolo(xmlp,os.path.join(saveroot,<span class="string">&quot;labels&quot;</span>,<span class="string">&quot;test&quot;</span>,file_id+<span class="string">&#x27;.txt&#x27;</span>),classes)</span><br><span class="line">                sh.copy(op,sp)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">maketxt</span>(<span class="params"><span class="built_in">dir</span>,saveroot,filename</span>):</span></span><br><span class="line">    savetxt = os.path.join(saveroot,filename)</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(savetxt,<span class="string">&#x27;w&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> tqdm(os.listdir(<span class="built_in">dir</span>)):</span><br><span class="line">            f.write(os.path.join(<span class="built_in">dir</span>,i)+<span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line">                           </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">toyolo</span>(<span class="params">xmlroot,imgroot,saveroot,errorId=[],classes=&#123;&#125;,tvp=<span class="number">1</span>,train_percent=<span class="number">0.9</span></span>):</span></span><br><span class="line">    <span class="comment"># toyolo main function</span></span><br><span class="line">    trainval(xmlroot,imgroot,saveroot,errorId,classes,tvp,train_percent)</span><br><span class="line">    maketxt(os.path.join(saveroot,<span class="string">&quot;images&quot;</span>,<span class="string">&quot;train&quot;</span>),saveroot,<span class="string">&quot;train.txt&quot;</span>)</span><br><span class="line">    maketxt(os.path.join(saveroot,<span class="string">&quot;images&quot;</span>,<span class="string">&quot;val&quot;</span>),saveroot,<span class="string">&quot;val.txt&quot;</span>)</span><br><span class="line">    maketxt(os.path.join(saveroot,<span class="string">&quot;images&quot;</span>,<span class="string">&quot;test&quot;</span>),saveroot,<span class="string">&quot;test.txt&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;Dataset to yolo format success.&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">test</span>():</span></span><br><span class="line">    box_root = <span class="string">&quot;E:/MyProject/Dataset/hwtest/annotations&quot;</span></span><br><span class="line">    image_root = <span class="string">&quot;E:/MyProject/Dataset/hwtest/images&quot;</span></span><br><span class="line">    output_root = <span class="string">&quot;E:/MyProject/Dataset/yolo&quot;</span></span><br><span class="line">    classes = &#123;<span class="string">&quot;liner&quot;</span>: <span class="number">0</span>,<span class="string">&quot;bulk carrier&quot;</span>: <span class="number">1</span>,<span class="string">&quot;warship&quot;</span>: <span class="number">2</span>,<span class="string">&quot;sailboat&quot;</span>: <span class="number">3</span>,<span class="string">&quot;canoe&quot;</span>: <span class="number">4</span>,<span class="string">&quot;container ship&quot;</span>: <span class="number">5</span>,<span class="string">&quot;fishing boat&quot;</span>: <span class="number">6</span>&#125;</span><br><span class="line">    errorId = []</span><br><span class="line">    train_percent = <span class="number">0.9</span></span><br><span class="line">    toyolo(box_root,image_root,output_root,errorId,classes,train_percent=train_percent)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    test()</span><br></pre></td></tr></table></figure><p>按照此脚本，将会在输出文件夹中生成以下内容：</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">-yolo/</span><br><span class="line">	|-images/</span><br><span class="line">		|-train/</span><br><span class="line">			|-1.jpg</span><br><span class="line">			|-2.jpg</span><br><span class="line">		|-test/</span><br><span class="line">			|-3.jpg</span><br><span class="line">			|-4.jpg</span><br><span class="line">		|-val/</span><br><span class="line">			|-5.jpg</span><br><span class="line">			|-6.jpg</span><br><span class="line">	|-labels/</span><br><span class="line">		|-train/</span><br><span class="line">			|-1.txt</span><br><span class="line">			|-2.txt</span><br><span class="line">		|-test/</span><br><span class="line">			|-3.txt</span><br><span class="line">			|-4.txt</span><br><span class="line">		|-val/</span><br><span class="line">			|-5.txt</span><br><span class="line">			|-6.txt</span><br><span class="line">	|-train.txt</span><br><span class="line">	|-test.txt</span><br><span class="line">	|-val.txt</span><br></pre></td></tr></table></figure></article><div class="post-reward"><div class="reward-button"><i class="fas fa-hamburger"></i> 打赏作者</div><div class="reward-main"><ul class="reward-all"><ul class="reward-group"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="" data-lazy-src="/img/wechat.jpg" alt="微信"></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="" data-lazy-src="/img/alipay.jpg" alt="支付宝"></a><div class="post-qr-code-desc">支付宝</div></li></ul><a class="reward-main-btn" href="/donate"><div class="reward-text">赞赏者名单</div><div class="reward-dec">因为你们的支持让我意识到写文章的价值🙏</div></a></ul></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a><a class="post-meta__tags" href="/tags/%E7%9B%AE%E6%A0%87%E6%A3%80%E6%B5%8B/">目标检测</a><a class="post-meta__tags" href="/tags/python/">python</a></div><div class="post_share"><div class="social-share" data-image="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/202109111517311.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload='this.media="all"'><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer="defer"></script></div></div><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-info">目标检测 | 常用数据集标注格式及生成脚本</span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="https://blog.justlovesmile.top/posts/865c56ba.html">https://blog.justlovesmile.top/posts/865c56ba.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="external nofollow noopener noreferrer">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://blog.justlovesmile.top" target="_blank">Justlovesmile's BLOG</a>！</span></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/e05a9ab6.html"><img class="prev-cover" src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/image-20211125175041150.png" onerror='onerror=null,src="/img/404.jpg"' alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Jupyter Lab | 安装、配置、插件推荐、多用户使用教程</div></div></a></div><div class="next-post pull-right"><a href="/posts/2bfb1caa.html"><img class="next-cover" src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/202108142119256.png" onerror='onerror=null,src="/img/404.jpg"' alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Hexo博客 | 动态分类标签条，自动获取全站分类与标签进行展示</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/bb608df3.html" title="目标检测 | RetinaNet，经典单阶段Anchor-Based目标检测模型"><img class="cover" src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/20220314113659.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-03-14</div><div class="title">目标检测 | RetinaNet，经典单阶段Anchor-Based目标检测模型</div></div></a></div><div><a href="/posts/fc798de3.html" title="目标检测 | Faster R-CNN，经典两阶段检测模型"><img class="cover" src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/20220312220823.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-03-12</div><div class="title">目标检测 | Faster R-CNN，经典两阶段检测模型</div></div></a></div><div><a href="/posts/bfa4054.html" title="深度学习 | 《深度学习入门之PyTorch》阅读笔记"><img class="cover" src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2@latest/post/pytorch.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-23</div><div class="title">深度学习 | 《深度学习入门之PyTorch》阅读笔记</div></div></a></div><div><a href="/posts/d150f284.html" title="深度学习 | 小样本学习基础概念"><img class="cover" src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/202201271037441.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-01-27</div><div class="title">深度学习 | 小样本学习基础概念</div></div></a></div><div><a href="/posts/6a054795.html" title="深度学习 | GAN，什么是生成对抗网络"><img class="cover" src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/20210226103604.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-03</div><div class="title">深度学习 | GAN，什么是生成对抗网络</div></div></a></div><div><a href="/posts/ebe3a70b.html" title="深度学习 | Wasserstein距离"><img class="cover" src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/20210131105502.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-31</div><div class="title">深度学习 | Wasserstein距离</div></div></a></div></div></div><hr><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i> <span>评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="" data-lazy-src="/img/avatar.jpg" onerror='this.onerror=null,this.src="/img/friend_404.gif"' alt="avatar"></div><div class="author-info__name">Justlovesmile</div><div class="author-info__description">一个计算机专业学生的个人博客，记录着学习笔记和生活中的思考，期待着和所有人相遇</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">75</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">69</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>不定时更新博客，欢迎交换<a href="/friends/"><strong>友链</strong></a>...</p><div class="twopeople"><div class="container" style="height:200px"><canvas class="illo" width="800" height="800" style="max-width:200px;max-height:200px;touch-action:none;width:640px;height:640px"></canvas></div><script src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN/js/twopeople1.js"></script><script src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN/js/zdog.dist.js"></script><script id="rendered-js" src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN/js/twopeople.js"></script><style>.twopeople{margin:0;align-items:center;justify-content:center;text-align:center}canvas{display:block;margin:0 auto;cursor:move}</style></div><div style="text-align:center"><a href="https://www.foreverblog.cn/" target="_blank" rel="external nofollow noopener noreferrer"><img src="" data-lazy-src="https://img.foreverblog.cn/logo_en_default.png" alt="foreverblog" style="width:auto;height:16px"></a></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-COCO"><span class="toc-text">1. COCO</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-COCO%E6%95%B0%E6%8D%AE%E9%9B%86%E6%A0%BC%E5%BC%8F"><span class="toc-text">1.1 COCO数据集格式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-COCO%E8%BD%AC%E6%8D%A2%E8%84%9A%E6%9C%AC"><span class="toc-text">1.2 COCO转换脚本</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-VOC"><span class="toc-text">2. VOC</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-VOC%E6%95%B0%E6%8D%AE%E9%9B%86%E6%A0%BC%E5%BC%8F"><span class="toc-text">2.1 VOC数据集格式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-VOC%E8%BD%AC%E6%8D%A2%E8%84%9A%E6%9C%AC"><span class="toc-text">2.2 VOC转换脚本</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-YOLO"><span class="toc-text">3. YOLO</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-YOLO%E6%95%B0%E6%8D%AE%E9%9B%86%E6%A0%BC%E5%BC%8F"><span class="toc-text">3.1 YOLO数据集格式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-YOLO%E8%BD%AC%E6%8D%A2%E8%84%9A%E6%9C%AC"><span class="toc-text">3.2 YOLO转换脚本</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/56b0563d.html" title="Hexo博客 | 如何让Butterfly主题导航栏居中"><img src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/20220315095300.png" onerror='this.onerror=null,this.src="/img/404.jpg"' alt="Hexo博客 | 如何让Butterfly主题导航栏居中"></a><div class="content"><a class="title" href="/posts/56b0563d.html" title="Hexo博客 | 如何让Butterfly主题导航栏居中">Hexo博客 | 如何让Butterfly主题导航栏居中</a><time datetime="2022-03-15T01:25:18.000Z" title="发表于 2022-03-15 09:25:18">2022-03-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/bb608df3.html" title="目标检测 | RetinaNet，经典单阶段Anchor-Based目标检测模型"><img src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/20220314113659.png" onerror='this.onerror=null,this.src="/img/404.jpg"' alt="目标检测 | RetinaNet，经典单阶段Anchor-Based目标检测模型"></a><div class="content"><a class="title" href="/posts/bb608df3.html" title="目标检测 | RetinaNet，经典单阶段Anchor-Based目标检测模型">目标检测 | RetinaNet，经典单阶段Anchor-Based目标检测模型</a><time datetime="2022-03-14T03:26:21.000Z" title="发表于 2022-03-14 11:26:21">2022-03-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/fc798de3.html" title="目标检测 | Faster R-CNN，经典两阶段检测模型"><img src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/20220312220823.png" onerror='this.onerror=null,this.src="/img/404.jpg"' alt="目标检测 | Faster R-CNN，经典两阶段检测模型"></a><div class="content"><a class="title" href="/posts/fc798de3.html" title="目标检测 | Faster R-CNN，经典两阶段检测模型">目标检测 | Faster R-CNN，经典两阶段检测模型</a><time datetime="2022-03-12T13:59:01.000Z" title="发表于 2022-03-12 21:59:01">2022-03-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/d150f284.html" title="深度学习 | 小样本学习基础概念"><img src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/202201271037441.png" onerror='this.onerror=null,this.src="/img/404.jpg"' alt="深度学习 | 小样本学习基础概念"></a><div class="content"><a class="title" href="/posts/d150f284.html" title="深度学习 | 小样本学习基础概念">深度学习 | 小样本学习基础概念</a><time datetime="2022-01-27T02:24:38.000Z" title="发表于 2022-01-27 10:24:38">2022-01-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/e05a9ab6.html" title="Jupyter Lab | 安装、配置、插件推荐、多用户使用教程"><img src="" data-lazy-src="https://cdn.jsdelivr.net/gh/Justlovesmile/CDN2/post/image-20211125175041150.png" onerror='this.onerror=null,this.src="/img/404.jpg"' alt="Jupyter Lab | 安装、配置、插件推荐、多用户使用教程"></a><div class="content"><a class="title" href="/posts/e05a9ab6.html" title="Jupyter Lab | 安装、配置、插件推荐、多用户使用教程">Jupyter Lab | 安装、配置、插件推荐、多用户使用教程</a><time datetime="2021-11-25T09:38:43.000Z" title="发表于 2021-11-25 17:38:43">2021-11-25</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div id="footer_deal"><a class="social-icon" href="mailto:865717150@qq.com" target="_blank" title="Email" rel="external nofollow noopener noreferrer"><i class="fas fa-envelope"></i></a><a class="social-icon" href="https://blog.csdn.net/qq_43701912" target="_blank" title="CSDN" rel="external nofollow noopener noreferrer"><i class="iconfont icon-csdn1"></i></a><a class="social-icon" href="https://github.com/Justlovesmile" target="_blank" title="Github" rel="external nofollow noopener noreferrer"><i class="fab fa-github"></i></a><a class="social-icon" href="https://weibo.com/u/5252319712" target="_blank" title="微博" rel="external nofollow noopener noreferrer"><i class="fa fa-weibo"></i></a><a class="social-icon" href="https://space.bilibili.com/168738824" target="_blank" title="Bilibili" rel="external nofollow noopener noreferrer"><i class="fas iconfont icon-bilibili"></i></a></div><div id="mj-footer"><div class="footer-group"><h3 class="footer-title">关于</h3><div class="footer-links"><a class="footer-item" target="_blank" rel="external nofollow noopener noreferrer" href="https://www.justlovesmile.top/">个人主页</a><a class="footer-item" href="/donate/">赞赏博主</a><a class="footer-item" href="/update/">博客日志</a><a class="footer-item" href="/charts/">博客统计</a></div></div><div class="footer-group"><h3 class="footer-title">归档</h3><div class="footer-links"><a class="footer-item" href="/archives/">文章归档</a><a class="footer-item" href="/tags/">全部标签</a><a class="footer-item" href="/categories/">全部分类</a><a class="footer-item" href="/random/">随机文章</a></div></div><div class="footer-group"><h3 class="footer-title">导航</h3><div class="footer-links"><a class="footer-item" href="/guestbook/">博客留言</a><a class="footer-item" href="/friends/">友情链接</a><a class="footer-item" href="/fcircle/">友链订阅</a><a class="footer-item" href="/atom.xml">RSS订阅</a></div></div><div class="footer-group"><h3 class="footer-title">协议</h3><div class="footer-links"><a class="footer-item" href="/privacy/">隐私协议</a><a class="footer-item" href="/cookies/">Cookies</a><a class="footer-item" href="/cc/">版权协议</a></div></div></div><div id="footer-banner"><div class="footer-banner-links"><div class="footer-banner-left"><div id="footer-banner-tips">©2019 - 2022 By Justlovesmile</div></div><div class="footer-banner-right"><a class="footer-banner-link" target="_blank" rel="external nofollow noopener noreferrer" href="http://beian.miit.gov.cn/">蜀ICP备20004960号</a><a class="footer-banner-link" href="/update/">主题</a><a class="footer-banner-link" href="/about/">关于</a></div></div></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i> <span>数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"></div></div><hr><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script defer="defer" src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script defer="defer" src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script defer="defer" src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script defer="defer" src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script defer="defer" src="/js/search/local-search.js"></script><div class="js-pjax"><script>(()=>{const t=()=>{twikoo.init(Object.assign({el:"#twikoo-wrap",envId:"blog-comment-3gt33nkmf9f97e6e",region:"ap-shanghai",onCommentLoaded:function(){btf.loadLightbox(document.querySelectorAll("#twikoo .tk-content img:not(.vemoji)"))}},null))},o=()=>{"object"!=typeof twikoo?getScript("https://cdn.jsdelivr.net/npm/twikoo/dist/twikoo.all.min.js").then(t):setTimeout(t,0)};btf.loadComment(document.getElementById("twikoo-wrap"),o)})()</script></div><canvas id="universe"></canvas><script defer="defer">console.log("\n %c 欢迎来到Justlovesmile の Blog %c https://github.com/Justlovesmile %c https://blog.justlovesmile.top \n","color: #f9ed69; background: #252a34; padding:5px 0;","background: #3fc1c9; padding:5px 0;","background: #3fc1c9; padding:5px 0;")</script><script defer="defer" src="/js/rgbaster.min.js"></script><script defer="defer" src="/js/justlovesmile.js"></script><script>window.addEventListener("load",async()=>{navigator.serviceWorker.register("/js/sw-cdn.js?time="+(new Date).getTime()).then(async e=>{"true"!=window.localStorage.getItem("install")&&(window.localStorage.setItem("install","true"),setTimeout(()=>{window.location.search="?time="+(new Date).getTime()},1e3))}).catch(e=>{console.log("sw-cdn.js error")})})</script></div></body></html>